package com.heima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.reggie.common.R;
import com.heima.reggie.pojo.Employee;
import com.heima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());


        String name = employee.getUsername();
        LambdaQueryWrapper<Employee> emp = new LambdaQueryWrapper<>();
        emp.eq(Employee::getUsername, name);
        // emp.eq(Employee::getPassword,password);


        Employee one = employeeService.getOne(emp);
        if (one == null || !one.getPassword().equals(password)) {
            return R.error("用户名有误或密码错误");
        }
//    if (! one.getPassword().equals(password) ) {
//        return R.error("密码错误");
//    }

        if (one.getStatus() == 0) {
            return R.error("已注销");
        }
        request.getSession().setAttribute("employee", one.getId());
        return R.success(one);


    }

    @PostMapping("/logout")
    public R logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");


        return R.success("成功");
    }

    @PostMapping
    public R<String> save(@RequestBody Employee employee, HttpServletRequest request) {
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//    employee.setCreateTime(LocalDateTime.now());
//    employee.setUpdateTime(LocalDateTime.now());
//   Long  a=(Long) request.getSession().getAttribute("employee");
//    employee.setCreateUser(a);
//    employee.setUpdateUser(a);
        employeeService.save(employee);
        return R.success("成功");

    }

    ;

    @GetMapping("/page")
    public R<Page> select(Integer page, Integer pageSize, String name) {
        Page<Employee> page2 = new Page(page, pageSize);

        LambdaQueryWrapper<Employee> lanma = new LambdaQueryWrapper<>();
        lanma.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        lanma.orderByDesc(Employee::getUpdateTime);


        Page<Employee> page3 = employeeService.page(page2, lanma);

        return R.success(page2);
    }

    @PutMapping
    public R undate(HttpServletRequest request, @RequestBody Employee employee) {
//   long employee1 = (long) request.getSession().getAttribute("employee");
//   employee.setUpdateUser(employee1);
//   employee.setUpdateTime(LocalDateTime.now());
        boolean b = employeeService.updateById(employee);


        return R.success("员工信息修改成功");
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable long id) {
        Employee byId = employeeService.getById(id);

        if (byId != null) {
            return R.success(byId);
        }

        return R.error("未知错误");
    }


}
